﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Web.Profile;
using IronOne.SalesOne.BusinessObjects;
using System.Configuration;
using Facade;
using System.Collections;
using System.Data;

namespace IronOne.SalesOne.SalesOneWeb.CustomerServices
{
    public partial class ViewComplaintDetails : SOSecurityPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                ValidateUserCredentials(Request.Url.LocalPath);
                string act = string.Empty;

                if (Request.QueryString["a"] != null)
                {
                    act = Request.QueryString["a"].ToString();
                }

                if (act == "v")
                {
                    drpStatus.Enabled = false;
                    txtRemarks.Enabled = false;
                    updateButton.Visible = false;
                }
                else if (act == "u")
                {
                    drpStatus.Enabled = true;
                    txtRemarks.Enabled = true;
                    updateButton.Visible = true;
                }


                if (!IsPostBack)
                {
                    ViewState["refUrl"] = Request.UrlReferrer.ToString();

                    CodeFacade fac = new CodeFacade();
                    List<ComplaintStatus> cs = fac.GetComplaintStatuss("CodeId ASC", 1);
                    ComplaintStatus cs1 = new ComplaintStatus();
                    cs1.CodeId = -1;
                    cs1.Description = "Select";
                    cs.Insert(0, cs1);
                    drpStatus.DataSource = cs;
                    drpStatus.DataValueField = "CodeId";
                    drpStatus.DataTextField = "Description";
                    drpStatus.DataBind();
                }

                if (Session["CompRow"] != null)
                {
                    GridViewRow clRow = (GridViewRow)Session["CompRow"];
                    lblCompId.Text = clRow.Cells[2].Text;
                    lblSoCode.Text = clRow.Cells[3].Text;
                    lblContractNo.Text = clRow.Cells[4].Text;
                    lblClientName.Text = clRow.Cells[5].Text;
                    lblClientNo.Text = clRow.Cells[6].Text;
                    lblAssignedDate.Text = clRow.Cells[8].Text;

                    drpStatus.SelectedValue = drpStatus.Items.FindByText(clRow.Cells[7].Text).Value;

                    lblClosingDate.Text = clRow.Cells[9].Text;
                    lblLstUpDate.Text = clRow.Cells[11].Text;
                    lblType.Text = clRow.Cells[12].Text;
                    lblSubType.Text = clRow.Cells[13].Text;
                    lblActionReq.Text = clRow.Cells[14].Text;
                    lblInformMethod.Text = clRow.Cells[15].Text;
                    if (!clRow.Cells[10].Text.Contains("&nbsp;"))
                    {
                        txtRemarks.Text = clRow.Cells[10].Text;
                    }


                    Session.Remove("CompRow");

                }
            }
            catch (Exception)
            {

            }
            
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void updateButton_Click(object sender, EventArgs e)
        {
            try
            {
                CustomerFacade cFac = new CustomerFacade();
                Complaint compl = cFac.GetComplaint(lblCompId.Text);

                if (drpStatus.SelectedItem.Text.Equals("FOLLOWED UP") || drpStatus.SelectedItem.Text.Equals("FOLLOWED UP"))
                {
                    compl.ComplaintStatus = int.Parse(drpStatus.SelectedValue);
                }
                
                compl.Remarks = txtRemarks.Text;

                int res = cFac.UpdateComplaint(compl);

                if (res == 1)
                {
                    Session["s"] = "1";  
                }
                else
                {
                    Session["f"] = "1";
                }

                Response.Redirect(ViewState["refUrl"].ToString(),false);
            }
            catch (Exception)
            {

                Session["f"] = "1";
            }
            
        }

        protected void cancelButton_Click(object sender, EventArgs e)
        {
            Response.Redirect(ViewState["refUrl"].ToString());
        }
    }
}
